package com.myproject.adapter;

import java.util.List;

import com.myproject.model.City;
import com.myproject.source.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CityAdapter extends ArrayAdapter<City> {
	private final Activity activity;
	private final List<City> cities;

	public CityAdapter(Activity activity, List<City> objects) {
		super(activity, R.layout.city_list_item, objects);
		this.activity = activity;
		this.cities = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		CityView cityView = null;

		if (rowView == null) {
			// Get a new instance of the row layout view
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.city_list_item, null);

			// Hold the view objects in an object,
			// so they don't need to be re-fetched
			cityView = new CityView();
			cityView.city_name = (TextView) rowView
					.findViewById(R.id.city_name);
			
			// Cache the view objects in the tag,
			// so they can be re-accessed later
			rowView.setTag(cityView);
		} else {
			cityView = (CityView) rowView.getTag();
		}

		// Transfer the stock data from the data object
		// to the view objects
		City currentcity = (City) cities.get(position);
		cityView.city_name.setText(currentcity.getCity_name());
		

		return rowView;
	}

	protected static class CityView {
		protected TextView city_name;
		
	}
}
